package com.delio.client.tipodocumentos;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.binding.FieldBinding;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.ButtonBar;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HiddenField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.filters.GridFilters;
import com.extjs.gxt.ui.client.widget.grid.filters.StringFilter;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;

public class TipoDocumentoForm extends ContentPanel{
	 Button addTipoDocumento;
		ToolBar toolBar;
		LayoutContainer cpc,cpw;
		BorderLayoutData sup,inf,east,center,filter,buttons,gridc;
		FormPanel fp,f;
		TextField<String> descripcion;
		TextField<String> descripcionf;
		HiddenField<String> id;
		private FormBinding formbinding;
		ComboBox<ModelData> cbcl,cbtd;
		Dialog dl; 
		
	public TipoDocumentoForm(){
		BorderLayout layout = new BorderLayout();
		setLayout(layout);
		addForm();	
		formulario();
	 	    
	    cpw = new ContentPanel();
	  
	    sup = new BorderLayoutData(LayoutRegion.NORTH);
        inf = new BorderLayoutData(LayoutRegion.SOUTH);
	    cpw.add(fp,sup);
	    cpw.add(contPanel(),inf);
	     
	    east = new BorderLayoutData(LayoutRegion.EAST,350);
	    east.setSplit(true);
	    east.setCollapsible(true);
	    
	    center  = new BorderLayoutData(LayoutRegion.CENTER);
	    center.setCollapsible(true);
	    
	    ContentPanel ctp = new ContentPanel();
	    ctp.setLayout(new BorderLayout());
	    ctp.setHeading("Listado de Tipos de Documento");
	    ctp.setIconStyle("images/icons/table.png");
	    ctp.setHeaderVisible(false);
	    ctp.setCollapsible(true);
	    
	    filter  = new BorderLayoutData(LayoutRegion.NORTH,25);
	    gridc   = new BorderLayoutData(LayoutRegion.CENTER);
	    buttons = new BorderLayoutData(LayoutRegion.SOUTH,25);
	    
	    TipoDocumentos.getPaginate();
		
        final PagingToolBar toolBarPag = new PagingToolBar(10);
        toolBarPag.bind(TipoDocumentos.loader);
        
        List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
        
        final CheckBoxSelectionModel<BeanModel> sm = new CheckBoxSelectionModel<BeanModel>();
       
        columns.add(sm.getColumn());
        columns.add(new ColumnConfig("descripcion", "Descripcion", 150));
        
        ColumnModel cm = new ColumnModel(columns);
        
        Grid<BeanModel> grid = new Grid<BeanModel>(TipoDocumentos.store, cm);  
        grid.addListener(Events.Attach, new Listener<GridEvent<BeanModel>>() {  
          public void handleEvent(GridEvent<BeanModel> be) {  
        	  TipoDocumentos.loader.load(0, 10);  
          }  
        }); 
        
        grid.setSelectionModel(sm);
        //grid.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
        grid.getSelectionModel().addListener(Events.SelectionChange,
                new Listener<SelectionChangedEvent<BeanModel>>() {

				@Override
				public void handleEvent(SelectionChangedEvent<BeanModel> be) {		
					  if (be.getSelection().size() > 0) {
	            		  formbinding.bind((ModelData) be.getSelection().get(0));  
		                } else {  
		                  formbinding.unbind();  
		                }
				}
        	
        });
        
        grid.setLoadMask(true);  
        grid.setBorders(true);               
       
        toolBar = new ToolBar(); 
        addTipoDocumento = new Button("Agregar", IconHelper.createPath("images/icons/new.gif"));
              
	
        
        GridFilters filters = new GridFilters();
      //  filters.getMessages().setFilterText(constants.filtros());
        
        StringFilter descripcionFilter = new StringFilter("descripcion");
        filters.addFilter(descripcionFilter);
        
        grid.addPlugin(filters);                
        grid.addPlugin(sm);
        
		setHeaderVisible(false);	
		formbinding = new FormBinding(fp, true); 
	    formbinding.addFieldBinding(new FieldBinding(id,"id"));
	    formbinding.addFieldBinding(new FieldBinding(descripcion,"descripcion"));
	
	    ctp.add(bbutonbar(),filter);
	    ctp.add(grid,gridc);
	    ctp.add(toolBarPag,buttons);
	       
	    add(ctp,center);
	    add(cpw,east);
	}
	
	
	public ContentPanel contPanel(){
		   ContentPanel cp  = new ContentPanel();
		   return cp;
	    }
		
		 public ButtonBar bbutonbar(){
			   dl = new Dialog();  
			   dl.setBodyBorder(false);  
			   dl.setHeading("Nuevo Tipo de Documento");  
			   dl.setWidth(330);  
			   dl.setHeight(200); 
			   
			  // ButtonBar btnbar = new ButtonBar();
			   Button salir  = new Button("Salir",IconHelper.create("images/icons/exit.png"));
			   Button guardar = new Button("Guardar",IconHelper.create("images/icons/save_all.png"));
			  
			   dl.setButtons("");
			   dl.addButton(salir);
			   dl.addButton(guardar);
				
			   dl.setFrame(true);

			   
			   salir.addSelectionListener(new SelectionListener<ButtonEvent>(){
				  public void componentSelected(ButtonEvent ce){
					  dl.hide();
				  }
			   });
			   guardar.addSelectionListener(new SelectionListener<ButtonEvent>(){
				   public void componentSelected(ButtonEvent ve){
	                  TipoDocumento td = new TipoDocumento();
	                  td.setDescripcion(descripcionf.getValue());
	                  TipoDocumentos.tdf = TipoDocumentoForm.this;
	                  TipoDocumentos.guardar(td);
	                  TipoDocumentos.loader.load();
	       		   }
			   });
			 		 	  
			  ButtonBar bb = new ButtonBar();
			  Button btnNew = new Button("Nuevo",IconHelper.createPath("images/icons/new.gif"));
			  Button btnUpdate = new Button("Actualizar",IconHelper.createPath("images/icons/update.png"));
			  Button btnEliminar = new Button("Eliminar",IconHelper.createPath("images/icons/delete.gif"));
			  bb.add(btnNew);
			  bb.add(btnUpdate);
			  bb.add(btnEliminar);
			  
			  btnNew.addSelectionListener(new SelectionListener<ButtonEvent>() {
			      public void componentSelected(ButtonEvent ce) {
			          dl.add(f);
			          dl.show(); 
				      }
			    });
			  
			  btnUpdate.addSelectionListener(new SelectionListener<ButtonEvent>(){
				  public void componentSelected(ButtonEvent be){
	                 TipoDocumento td = new TipoDocumento();
	                 td.setId(Integer.parseInt(id.getValue()));
	                 td.setDescripcion(descripcion.getValue());
	                 TipoDocumentos.actualizar(td);
	                 TipoDocumentos.loader.load();
				  }
			  });
			  
			  btnEliminar.addSelectionListener(new SelectionListener<ButtonEvent>(){
				 public void componentSelected(ButtonEvent ce){
					 if(id.getValue() != null){
						 TipoDocumentos.eliminar(Integer.parseInt(id.getValue()));
						 TipoDocumentos.loader.load();
					 }else{
						 MessageBox.alert("Seleccionar","Seleccione la fila porfavor", null);	 
					 }
				 }
			  });
			  
			  return bb;
		  }

		 public void addForm(){
			fp = new FormPanel();
			id = new HiddenField<String>();
			id.setVisible(false);
			descripcion = new TextField<String>();
			descripcion.setFieldLabel("Descripcion");
			fp.add(id);
			fp.add(descripcion);
	    
	  	 }
		 
		 
		 public void formulario(){
		    f = new FormPanel();
		    f.setHeaderVisible(false);
		 	descripcionf = new TextField<String>();
			descripcionf.setFieldLabel("Descripcion");
			f.add(descripcionf);
		 }
			 
		 public void clearForm(){
			 descripcionf.clear();
		 }

}